// @ts-nocheck

const list = [
  {
    id: 1,
    type: 1,
    name: "发起人",
    pre: 0,
    details: [
      {
        pre: 0,
        next: 2,
      },
      {
        pre: 0,
        next: 3,
      },
    ],
  },
  {
    id: 2,
    type: 2,
    name: "条件1",
    details: [
      {
        pre: 1,
        next: 4,
      },
    ],
  },
  {
    id: 3,
    type: 2,
    name: "条件2",
    details: [
      {
        pre: 1,
        next: 7,
      },
    ],
  },
  {
    id: 4,
    type: 3,
    name: "审核人1",
    details: [
      {
        pre: 2,
        next: 5,
      },
      {
        pre: 2,
        next: 6,
      },
    ],
  },
  {
    id: 5,
    type: 2,
    name: "条件3",
    details: [{ pre: 4, next: 7 }],
  },
  {
    id: 6,
    type: 2,
    name: "条件4",
    details: [{ pre: 4, next: 7 }],
  },
  {
    id: 7,
    type: 4,
    name: "审核人2",
    details: [
      {
        pre: 3,
        next: 8,
      },
      {
        pre: 5,
        next: 8,
      },
      {
        pre: 6,
        next: 8,
      },
    ],
  },
  {
    id: 8,
    type: 4,
    name: "环境",
    details: [
      {
        pre: 7,
        next: 9,
      },
    ],
  },
  {
    id: 9,
    type: 4,
    name: "结束",
    details: [
      {
        pre: 8,
        next: 0,
      },
    ],
  },
];
const gg = (data: any, details1: any) => {
  const { details } = data;
  if (typeof details !== "object" || !details.length) return data;
  //判断是否下级关联的是否是条件
  let nextNode = list.find((ele) => ele.id === details[0].next);
  if (!nextNode) return data;
  if (nextNode?.type === 2) {
    let nextNode2 = list.find((ele) => ele.id === details[1].next);
    //条件要找到同级结束点
    data.childNode = digui(nextNode, nextNode2);
    if (data.childNode) {
      data.childNode = gg(data.childNode);
    }
    details.forEach((detail, index) => {
      if (!data?.conditions) data.conditions = [];
      const conditionNode = list.find((val) => val.id === detail.next);
      data.conditions.push(conditionNode);
      data.conditions[index] = gg(conditionNode);
    });
  } else {
    // data.childNode = nextNode;
    //如果此节点被记录过，则放回空
    if (flagIds.indexOf(nextNode.id) >= 0) return data;
    flagIds.push(nextNode.id);
    data.childNode = gg(nextNode);
  }

  return data;
};
const digui = (child1: any, child2: any) => {
  const result = fact(child1, child2);
  //如果没有找到，继续往下找
  if (!result) {
    const nextId = child2.details[0].next;
    if (!nextId) return;
    const nextNode = list.find((item) => item.id === nextId);
    digui(child1, nextNode);
  }
  return result;
};
const flagIds = [];
//找两个节点的汇合节点
const fact = (child1: any, child2: any) => {
  const nextId = child1.details[0].next;
  if (!nextId) return;
  const nextNode = list.find((item) => item.id === nextId);
  //如果next同时有child1和child2，则为汇合
  if (nextNode?.details.length > 1) {
    //获取节点的所有上节点
    const index1 = nextNode?.details.findIndex((val) => val.pre === child1.id);
    const index2 = nextNode?.details.findIndex((val) => val.pre === child2.id);
    if (index1 > -1 && index2 > -1) {
      //汇合点
      console.log("汇合", nextNode);
      //如果此节点被记录过，则放回空
      if (flagIds.indexOf(nextId) >= 0) return;
      flagIds.push(nextId);
      return nextNode;
    }
  }
  return fact(nextNode, child2);
};
export { gg, list };
